/*
 * @Author: xiaosihan
 * @Date: 2023-02-06 15:22:57
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-10-23 20:35:35
 */
import { AmbientLight, DirectionalLight, PointLight, Vector3 } from 'three';
import ThreeBase from 'three-base';
import MeshBase from 'three-base/MeshBase';
import Sky from 'three-base/Sky';
import threeUtils from 'three-base/threeUtils';
/**
 * @class HomeRender
 * @extends {ThreeBase}
 */
class HomeRender extends ThreeBase {
  constructor() {
    super();
  }

  // 环境光
  ambientLight = (() => {
    const ambientLight = new AmbientLight('#fff', 1.5);
    this.scene.add(ambientLight);
    return ambientLight;
  })();

  //点光源
  pointLight = (() => {
    const pointLight = new PointLight('#fff', 1.5);
    pointLight.position.set(50, 40, 50);
    pointLight.shadow.bias = -0.001;
    this.scene.add(pointLight);
    return pointLight;
  })();

  // 方向光
  directionalLight = (() => {
    const directionalLight = new DirectionalLight('#fff', 1.5);
    directionalLight.position.set(-10, 10, 5);
    directionalLight.target.position.set(0, 0, 0);
    directionalLight.castShadow = true;
    directionalLight.shadow.bias = -0.001;
    directionalLight.shadow.mapSize.set(1024, 1024);
    this.scene.add(directionalLight);
    return directionalLight;
  })();

  init() {
    super.init();

    // 开发模式时还原记录的位置
    if (threeUtils.isDev && localStorage.lookAt) {
      this.resetLookAt({
        ...JSON.parse(localStorage.lookAt),
      });
    }

    // 开发模式时记录当前的位置
    if (threeUtils.isDev) {
      this.controls.addEventListener('change', () => {
        localStorage.lookAt = JSON.stringify(this.getLookAt());
      });
    }

    if (threeUtils.isProd || (threeUtils.isDev && !localStorage.lookAt)) {
      this.resetLookAt({
        center: new Vector3(0, 0, 0),
        position: new Vector3(-7.049, 27.469, 59.276),
      });
    }
  }

  // 天空
  sky = (() => {
    const sky = new Sky();
    this.add(sky);
    return sky;
  })();

  //   当归
  dangguiMesh = (() => {
    const dangguiMesh = new MeshBase('./当归.glb');
    dangguiMesh.addEventListener('loaded', () => {
      this.lookatObjects({
        objects: [dangguiMesh],
        direction: new Vector3(0, 0, 1),
        duration: 0,
      });
    });
    this.add(dangguiMesh);
    return dangguiMesh;
  })();
}

const homeRenderer = (window.homeRenderer = new HomeRender());

export default homeRenderer;
